package com.xxgc.cloud.auth.springcloudauth.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;

/**
 * @program: SpringCloudStudy
 * @description: 逆向成功代码生成器
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-11-22 15:07
 **/
public class CodeGenerator {

    private static String[] modelUrls = new String[]{
            // 0 认证模块
            "\\spring-cloud-auth",
            // 1 商品模块
            "\\spring-cloud-modules\\spring-cloud-modules-consumer\\spring-cloud-consumer-goods"
    };

    private static String[] packageNames = new String[]{
            "com.xxgc.cloud.auth.springcloudauth",
            "com.xxgc.cloud.modules.springcloudconsumergoods"
    };


    public static void main(String[] args) {
        //当前使用
        int thisNum = 1;


        //获取工程地址
        String systemUrlJava = System.getProperty("user.dir")+modelUrls[thisNum]+"\\src\\main\\java\\";
        String systemUrlMapper = System.getProperty("user.dir")+packageNames[thisNum]+"\\src\\main\\resources\\mapper\\";
        //数据库地址
        String url = "jdbc:mysql://localhost:3306/spring_cloud_choutuan?serverTimezone=GMT%2b8";
        String username = "root";
        String password = "123456";


        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("Mr.Abel") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir() //禁止打开输出目录
                            .outputDir(systemUrlJava); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(packageNames[thisNum]) // 设置父包名
                            .moduleName(null) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, systemUrlMapper)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok().addTableFills(
                            new Column("create_time", FieldFill.INSERT),
                            new Column("update_time", FieldFill.INSERT_UPDATE)
                    ); //启动lombok
                    builder.mapperBuilder().enableMapperAnnotation().build(); //启用@mapper注释
                    builder.controllerBuilder().enableHyphenStyle().enableRestStyle(); //启用驼峰转连字符样式
                    builder.addInclude("goods");
                })
                .templateConfig(builder -> {
                    //builder.controller("/templates/ controller.java.vm");//模板
                    builder.disable(TemplateType.CONTROLLER);//不生成前端控制器
                    builder.disable(TemplateType.SERVICE);//不生成Service
                    builder.disable(TemplateType.SERVICEIMPL);//不生成ServiceImpl
                })
                .execute();
    }
}
